package util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
	@SuppressWarnings("deprecation")
	public class HibernateUtil {
		private static final SessionFactory sessionFactory;
		
		private HibernateUtil(){
		}
		
		static{
			try{
				Configuration cfg = new Configuration().configure();
				ServiceRegistryBuilder regbulider=new ServiceRegistryBuilder().applySettings(cfg.getProperties());
				ServiceRegistry reg=regbulider.buildServiceRegistry();
				sessionFactory = cfg.buildSessionFactory(reg);
			}
			catch(HibernateException ex){
				throw new RuntimeException("创建SessionFactory失败: " + ex.getMessage(), ex);
			}
		}
		
		public static SessionFactory getSessionFactory() {
		  	return sessionFactory;
		}
		
		public static Session getSession() throws HibernateException {    
	        return sessionFactory.openSession();  
	    }

	
}